define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 修改添加窗口的大小
            $(".btn-add").data("area", ["50%", "75%"]);
            // 修改编辑窗口的大小
            $(".btn-edit").data("area", ["50%", "75%"]);

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/coupon/index' + location.search,
                    add_url: 'shop/coupon/add',
                    edit_url: 'shop/coupon/edit',
                    del_url: 'shop/coupon/del',
                    multi_url: 'shop/coupon/multi',
                    import_url: 'shop/coupon/import',
                    table: 'shop_coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                singleSelect:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shop_id', title: __('店铺ID')},
                        {field: 'shop.name', title: __('店铺名称'), operate: 'LIKE'},
                        {field: 'name', title: __('券名'), operate: 'LIKE'},
                        {field: 'amount', title: __('满减金额'), operate:false},
                        {field: 'order_amount', title: __('订单金额'), operate:false},
                        {field: 'max_count', title: __('最大份数'), operate:false},
                        {field: 'valid_day', title: __('有效天数'), operate:false},
                        {field: 'room', title: __('绑定包厢'), operate:false, formatter: function (value, row, index, field) {
                                if (value == null) {
                                    return "";
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.room + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "200px"
                                    }
                                };
                            }},
                        {field: 'week', title: __('所属星期'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'time', title: __('使用时间'), operate:false},
                        {field: 'type', title: __('Type'), searchList: {"user":__('Type user'),"order":__('Type order'),"normal":__('Type normal')}, formatter: Table.api.formatter.normal},
                        {field: 'is_sys', title: __('Is_sys'), searchList: {"0":__('Is_sys 0'),"1":__('Is_sys 1')}, operate:'FIND_IN_SET', formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {"doing":__('Status doing'),"stop":__('Status stop')}, formatter: Table.api.formatter.status},
                        // {field: 'rule', title: __('Rule'), operate: 'LIKE'},
                        {field: 'create_time', title: __('创建时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: '发放满减券',
                                    text: '发放满减券',
                                    title: '发放满减券',
                                    confirm: "是否确认发放满减券",
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: 'fa fa-send-o',
                                    visible: function (row) {
                                        if (row.is_sys === '0') {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    url: '/admin/shop/coupon/sendCoupon',
                                    refresh: true
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            table.on('post-body.bs.table', function () {
                $(".btn-editone").data("area", ["60%", "80%"]);
                $(".btn-addone").data("area", ["60%", "80%"]);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            //同步包厢
            $("#c-room_id").data("params", function () {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-room_id").data("params", function () {
                return {custom: {shop_id: $("#c-shop_id").val()}};
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
